package com.dinadi.yidi.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.MyCouponsStateRequestEntity;
import com.dinadi.yidi.bean.OrderDetailsEntity;
import com.dinadi.yidi.bean.OrderDetailsRequest;
import com.dinadi.yidi.ui.activity.ProductOrderDetailsActivity;
import com.dinadi.yidi.ui.view.MyListView;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/22
 * Time: 15:19
 */

public class MyOrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    Context mContext;
    List<OrderDetailsEntity> mDatas = new ArrayList<>();
    /*运用典型的观察者模式添加点击事件*/
    /*定义接口变量*/
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;


    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, String orderId);
    }


    public void addAllData(List<OrderDetailsEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
        notifyDataSetChanged();
    }

    public MyOrderAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_my_order_item, parent, false);
        //将创建的View注册点击事件
        v.setOnClickListener(this);
        return new MyOrderAdapter.ItemViewHolder(v);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.lv_product.setAdapter(new MyOrderProductAdapter(position, mDatas.get(position).getOrderDetails()));
        itemViewHolder.lv_product.setDivider(null);
        itemViewHolder.lv_product.setFocusable(false);
        itemViewHolder.lv_product.setClickable(false);
        itemViewHolder.lv_product.setPressed(false);
        itemViewHolder.lv_product.setEnabled(false);
        itemViewHolder.tv_order_number.setText(mDatas.get(position).getOrderId());

        itemViewHolder.product_num.setText("共" + mDatas.get(position).getProductNum() + "件商品");
        itemViewHolder.product_price.setText(mDatas.get(position).getPayPrice());
        String status = mDatas.get(position).getStatus();
        /*订单状态1-未付款 2-已付款 3-已发货 4-确认收货 7-申请退款 8-已退款 9-已取消*/
        if (status.equals("1")) {
            itemViewHolder.logistics_status.setText(R.string.text_un_paid);
        } else if (status.equals("2")) {
            itemViewHolder.logistics_status.setText(R.string.text_paid);
        } else if (status.equals("3")) {
            itemViewHolder.logistics_status.setText(R.string.text_shipped);
        } else if (status.equals("4")) {
            itemViewHolder.logistics_status.setText(R.string.text_successful_trade);
        } else if (status.equals("7")) {
            itemViewHolder.logistics_status.setText(R.string.text_pply_or_refund);
        } else if (status.equals("8")) {
            itemViewHolder.logistics_status.setText(R.string.text_refund);
        } else if (status.equals("9")) {
            itemViewHolder.logistics_status.setText(R.string.text_tansaction_losed);
        }

/*        itemViewHolder.layout_order_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, ProductOrderDetailsActivity.class);
                intent.putExtra("orderId", mDatas.get(position).getOrderId());
                mContext.startActivity(intent);
            }
        });*/

        holder.itemView.setTag(mDatas.get(position).getOrderId());

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView tv_order_number, logistics_status, product_num, product_price;

        LinearLayout layout_order_details;
        MyListView lv_product;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tv_order_number = (TextView) itemView.findViewById(R.id.tv_order_number);
            logistics_status = (TextView) itemView.findViewById(R.id.logistics_status);
            product_num = (TextView) itemView.findViewById(R.id.product_num);
            product_price = (TextView) itemView.findViewById(R.id.product_price);
            layout_order_details = (LinearLayout) itemView.findViewById(R.id.layout_order_details);
            lv_product = (MyListView) itemView.findViewById(R.id.lv_produce);

        }

    }


    private class MyOrderProductAdapter extends BaseAdapter {

        private int position;
        private List<OrderDetailsRequest> mList;

        public MyOrderProductAdapter(int position, List<OrderDetailsRequest> orderDetails) {
            this.position = position;
            this.mList = orderDetails;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // 把布局加进来
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = View.inflate(mContext, R.layout.item_order, null);
                viewHolder.img_product = (ImageView) convertView.findViewById(R.id.img_product);
                viewHolder.tv_product_name = (TextView) convertView.findViewById(R.id.tv_product_name);
                viewHolder.layout_item = (RelativeLayout) convertView.findViewById(R.id.layout_item);

                String productName = mList.get(position).getProductTitle();
                viewHolder.tv_product_name.setText(productName);

                String imgUrl = mList.get(position).getCoverImg();
                if (imgUrl != null) {
                    if (!imgUrl.equals("")) {
                        ImageUtils.loadImageView(mContext, imgUrl, viewHolder.img_product);
                    } else {
                        ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, viewHolder.img_product);
                    }
                } else {
                    ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, viewHolder.img_product);

                }
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }


            return convertView;
        }


        class ViewHolder {
            ImageView img_product;
            TextView tv_product_name;
            RelativeLayout layout_item;


        }
    }

    /*将事件转移给外面的调用者*/
    @Override
    public void onClick(View v) {

        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (String) v.getTag());

        }
    }

    /*最后暴露给外面的调用者*/
    public void setmOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }

}

